package nz.ac.massey.cs.se400_2013.ass3;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

public class SimpleObjectStore {
	
	private static final String DB = "data1.xml";
	public static Collection<Course> COURSES = new ArrayList<Course>();
	
	
	
	static {
		try {
			FileInputStream in = new FileInputStream(DB);
			XMLDecoder decoder = new XMLDecoder(in);
			COURSES = (Collection<Course>)decoder.readObject();
			in.close();
			System.out.println("Data loaded from " + DB);
		}
		catch (Exception x) {
			System.err.println("Cannot load data from " + DB);
			x.printStackTrace();
		}
	}
	
	public void save() throws IOException {
		FileOutputStream out = new FileOutputStream(DB);
		XMLEncoder encoder = new XMLEncoder(out);
		encoder.writeObject(COURSES);
		System.out.println("Data written to " + DB);
	}
	
	
	
}
